

from flask import request
from flask_socketio import SocketIO, emit, join_room, leave_room
from utils.randomUtils import  generate_random_by_time
import threading
from .baseGraphNode import BaseGraphNode
from .sessionInfo import SessionInfo

class HttpOutGraphNode(BaseGraphNode):
    
    # 存储 SocketIO 实例的字典，允许动态创建新的 SocketIO 应用
    socketio_instances = {}
    def __init__(self, id:str, data:dict, graphNodeManager, random_key:str):
        super().__init__(id, data, graphNodeManager, random_key)
        self.route = data['data']['route']
        self.running = False

    def execute(self, session_info: SessionInfo=None):
        assert session_info is not None, "session_info should not be None when return message"
        # return-msg
        returnMsg = ''
        if 'return-msg' in self.dict['in']: # {'nodeId': sourceID, 'handle': sourceHandle}
            msgFrom = self.dict['in']['return-msg']
            prenode =self.graphNodeManager.nodesByIdDict[msgFrom['nodeId']]
            returnMsg = prenode.dict['out'][session_info.id][msgFrom['handle']][-1]
        print(f'returnMsg: {returnMsg}')
        session_info.ws.emit(self.route, {'data': returnMsg}, room=session_info.sid, namespace=session_info.full_url)
        return session_info.full_url
    
